window.addEvent('domready',function(){
	$('kx_gallery_inline').getElements('img').reflect();
	
	var opacity = 80/100;
	var height = 1;
	var radiusx = 10;
	var radiusy = 20;
	var centerx = 120;
	var centery = 90;
	var base = 0.15;
	var speed = 0.01;
	var clickit = true;
	var counter = 1;

	var mzove = function (direction) {
		var posx = 10;
		var posy = 0;
		elements.each(function(photo, i) {
			$(photo).setStyles({ left: direction + posx + 'px', top: direction + posy + 'px'});
		});
	};

//	var myFx = new Fx.Tween($('gallery-preview'));
//	//Transitions the background color of the Element from black to red:
//	myFx.start('background-color', '#000', '#f00');
//	//Transitions the background color of the Element from its current color to blue:
//	myFx.start('background-color', '#00f');
	
//	var myEffect = new Fx.Morph($('gallery-preview'), {duration: 'long', transition: Fx.Transitions.Sine.easeOut});
//	 
//	myEffect.start({
//	    'height': [10, 100], //Morphs the 'height' style from 10px to 100px.
//	    'width': [900, 300]  //Morphs the 'width' style from 900px to 300px.
//	});
var GalleryAlbum = new Class({
	Implements: Options,
    options: {
		duration: 6000
	},

	initialize: function(element, options){
		this.setOptions(options);
		this.element = $(element);
		this.element.addEvent('mousewheel', this.onMouseWheel.bind(this));
		this.elements = this.element.getElements('.kx_item');
		if (this.elements.length > 1) {
			setInterval(this.next.bind(this), this.options.duration);
		}
	},
	
	next: function() {
		if (undefined == this.count || this.count < 0) {
			this.count = this.elements.length;
		} else
		if (this.count > this.elements.length) {
			this.count = this.elements.length;
		}
		if(this.elements.contains(this.count--)) {
			this.elements[this.count--].fade('in');
			this.elements.fade('hide');
		}
	},
	
	prev: function() {
		if (undefined == this.count || this.count < 0) {
			this.count = 0;
		} else
		if (this.count >= this.elements.length) {
			this.count = 0;
		}
		if(this.elements.contains(this.count++)) {
			this.elements[this.count++].fade('in');
			this.elements.fade('hide');
		}
	},

    onMouseWheel: function(e) {
    	if (e.wheel > 0) {
			this.next();
		} else
		if (e.wheel < 0) {
			this.prev();
		}
    }
});

var Gallery = new Class({
	Implements: Options,
    options: {
		id: 'kx_item',
		move: 100,
		wheelSens: 200,
		morphOptions: {duration: 1000, transition: Fx.Transitions.Sine.easeOut}
	},

    initialize: function(element, options){
        this.setOptions(options);
        this.element = $(element);
        this.element.addEvent('mouseout', this.onHover.bind(this));
        this.element.addEvent('mouseover', this.onHover.bind(this));
        this.element.addEvent('click', this.onSelect.bind(this));
    },

    start: function() {
    	this.albums = [];

    	this.elements = this.element.getElements('.album');
    	this.elements.each((function(album, i) {
    		this.albums[i] = new GalleryAlbum(album);
    	}).bind(this));
    },
    onHover: function() {
    	this.element.hasClass('more')
    		? this.element.removeClass('more')
    		: this.element.addClass('more');
    	
    },

    onSelect: function(e) {
    	e.preventDefault();

    	this.element.addClass('select');
    }
});

elements = $('kx_gallery_inline').getElements('.kx_gallery_album');
elements.each(function(gallery, i) {
	var gallery = new Gallery(gallery);
	gallery.start();
});

var CircledElements = new Class({
	Implements: Options,
    options: {
		centerx: 100,
		centery: 100,
		radiusx: 10,
		radiusy: 10,
		angle:0,
		baseAngle:0,
		rotateAngle: null,
		onRun: $empty
	},
    
    initialize: function(elemens, options){
    	this.setOptions(options);
    	this.elements = elements;
    },
    
	// inicjowanie
	run: function() {
		// ilosc obracanych elementow
		var n = this.elements.length;
		// kąto o jaki przesuwamy
		var rotateAngle = Math.PI*2/n;
		// .. domyslny kąt
		if (this.options.rotateAngle != null) {
			if (parseFloat(this.options.rotateAngle) == this.options.rotateAngle) {
				rotateAngle = this.options.rotateAngle;
			}
		}

		var currentX = 0;
		var currentY = 0;
		var currentAngle = 0;

		// generowanie nowych wsp. elementu
		for (var i = 0; i < n; i++) {
			// kont o jaki wektor jest obracany
			currentAngle = rotateAngle*(i+1) + this.options.angle;
			currentAngle += this.options.baseAngle;

			// przesuniecie wektora o kont i odleglosc 
			currentX = this.options.centery + (this.options.radiusy * Math.cos(currentAngle));
			currentY = this.options.centerx + (this.options.radiusx * Math.sin(currentAngle));

			// wywolanie funkcji na kazdym elemencie ..
			if (typeof this.options.onRun == 'function') {
				this.options.onRun(this.elements[i], currentX, currentY, i);
			}
		}
	},
	setAngle: function (angle) {this.options.angle = parseInt(angle)},
	setRadius: function (radius) {
		this.options.radiusx = parseInt(radius);
		this.options.radiusy = parseInt(radius);
	},
	setRadiusX: function (radius) {this.options.radiusx = parseInt(radius)},
	setRadiusY: function (radius) {this.options.radiusy = parseInt(radius)},
});

var circled = new CircledElements(elements, {
	centerx: 380,
	centery: 70,
	radiusx: 380,
	radiusy: 100,
	baseAngle: 11,
	onRun: function(element,x,y,index){
		element.morph({
			top  : x + 'px',
			left : y + 'px',
//			zIndex: Math.round(y),
		}, 5000);
//		, {transition: Fx.Transitions.Elastic.easeOut});
	}
});

var angle = 0;
var direction = 1;
circled.run();

var move = function() {
	if (direction > 0) {
		if (angle >= 360) {
			angle = 0;
		} else
		if (angle <= 0) {
			angle = 0;
		}
		angle++;
	} else
	if (direction < 0) {
		if (angle >= 360) {
			angle = 360;
		} else
		if (angle <= 0) {
			angle = 360;
		}
		angle--;
	}
	circled.setAngle(angle);
	circled.run();
};

$('gallery-preview').addEvent('mousewheel', function(e){
	direction = e.wheel;
	move()
});

b = setInterval(move, 5000);

});